package com.study.algorithm.datastruct.tree.impl;

import com.study.algorithm.datastruct.tree.model.BinaryTree;

public class TreeBaseOperate {

    protected int getDeepForRootNodeOrJudgeBalance(BinaryTree binaryTree, boolean isJudgeBalance) {
        int deep = 0;
        int notBalance = -1;
        int leftDeep;
        int rightDeep;
        if (binaryTree == null) {
            return deep;
        }
        deep += 1;//如果该节点不是空节点，即标记该节点深度加1
        leftDeep = getDeepForRootNodeOrJudgeBalance(binaryTree.getLeftChildNode(), isJudgeBalance);
        if (isJudgeBalance && leftDeep == notBalance) {
            return notBalance;
        }
        rightDeep = getDeepForRootNodeOrJudgeBalance(binaryTree.getRightChildNode(), isJudgeBalance);
        if (isJudgeBalance && (rightDeep == notBalance || Math.abs(leftDeep - rightDeep) > 1)) {
            return notBalance;
        }
        deep += leftDeep > rightDeep ? leftDeep : rightDeep;
        return deep;
    }

}
